#!/usr/bin/perl
#save_share.cgi

require './shareman-lib.pl';

&ReadParse();
&error_setup($text{'save_share_error'});
my $emode = $in{'edit'};
my ($uglist, @ulist, %pw, %sd, %gp, $err_msg);

$in{'sharetype'}=~ s/\W//g;
$anonypath = undef;
if (($emode =~ /create/) || ($emode =~ /property/))
{
#&header($text{'shares_title'}, "", undef, 0, 1);
	if (!$in{'sharepath'})
	{
		&nas_syslog("shareman", "naslog_warning", 31004, "Share path is empty!");
		&error($text{'sharepath_empty'});
	}
	if (!$in{'sharename'})
	{
		&nas_syslog("shareman", "naslog_warning", 31005, "Share name is empty!");
		&error($text{'sharename_empty'});
	}
	if ($in{'sharename'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)]/)
	{
		&nas_syslog("shareman", "naslog_warning", 31025, "Invalid share name!");
		&error($text{'sharename_invalid'});	
	}
	
	if ($in{'sharepath'} =~ /[^A-Za-z0-9\x7f-\xff\+\_\-\(\)\/]/)
	{
		&nas_syslog("shareman", "naslog_warning", 31025, "Share path invalid!");
		&error($text{'sharepath_invalid'});
	}

	if (length($in{'sharename'}) > 23)
	{
		&nas_syslog("shareman", "naslog_warning", 31026, "Share name too long!");
		&error($text{'sharename_toolong'});
	}
	
	if (!$in{'sharetype'})
	{
		&nas_syslog("shareman", "naslog_warning", 31006, "Share type is empty!");
		&error($text{'sharetype_empty'});
	}
	if ($emode =~ /create/)
	{
		$mntroot = &get_mnt_root();
		if($mntroot eq "")
		{
			&nas_syslog("shareman", "naslog_warning", 31007, "There are no valid volumes!");
			&error($text{'invalid_volumes'});
		}
		if ($in{'sharepath'} !~ /^\//)
		{
			$in{'sharepath'} = "/".$in{'sharepath'};
		}
		if ($in{'sharepath'} =~ /\/\//)
		{
			&nas_syslog("shareman", "naslog_warning", 31026, "Share path invalid!");
			&error($text{'error_msg_sharepath_moredash'});	
		}
		if ($in{'sharepath'} =~ /^(.*)\/+$/)
		{
			$in{'sharepath'} = $1;
		}
		if ($in{'sharepath'} =~ /[^A-Za-z0-9\x7f-\xff\+\_\-\(\)\/]/)
		{
			&nas_syslog("shareman", "naslog_warning", 31025, "Share path invalid!");
			&error($text{'sharepath_invalid'});
		}

		unless($in{'sharepath'})
		{
			&nas_syslog("shareman", "naslog_warning", $text{'log_ev_sharepath_root'}, &text('log_msg_sharepath_root', $mntroot.$in{'selvol'}.'/'));
			&error(&text('error_msg_sharepath_root', $mntroot.$in{'selvol'}.'/'));
		}
			
		$referpath = $in{'sharepath'};
		$in{'sharepath'} = $mntroot.$in{'selvol'}.$in{'sharepath'};
	}

	($a, $b) = &check_sharetab($in{'sharepath'}, $in{'sharename'});
	if ($emode =~ /create/)
	{
		if ($a)
		{
			&nas_syslog("shareman", "naslog_warning", 31008, &text('sharepath_already_tab', $in{'sharepath'}));
			&error(&text('sharepath_already_tab', $in{'sharepath'}));
		}
		if ($b)
		{
			&nas_syslog("shareman", "naslog_warning", 31009, &text('sharename_already_tab', $in{'sharename'}));
			&error(&text('sharename_already_tab', $in{'sharename'}));
		}
		if (!(-e $in{'sharepath'}))
		{
			if ($in{'createfolder'} eq 'y')
			{
				my @paths = split("\/", $referpath);	
				my $cmsk = 0777;
				my $oldmask = umask 0000;
				$origpath = $mntroot.$in{'selvol'};
				for($ct = 0;$ct < @paths; $ct ++)
				{
					$origpath = $origpath."\/".$paths[$ct];
					if (!(-e $origpath))
					{
						$suc = mkdir($origpath, $cmsk);
					}	
				}
#				$suc = mkdir($in{'sharepath'}, $cmsk);
				umask $oldmask;
				if (!$suc)
				{
					&nas_syslog("shareman", "naslog_warning", 31010, &text('create_folder_fail', "$loc$fname"));
					&error(&text('create_folder_fail', "$loc$fname"));
				}

			}
			else
			{
				&nas_syslog("shareman", "naslog_warning", 31011, &text('sharepath_notexist', $in{'sharepath'}));
				&error(&text('sharepath_notexist', $in{'sharepath'}));
			}
		}
	}
	else
	{
		#&error(&text('sharepath_notexist_tab', $in{'sharepath'})) if (!$a);
		if ($b)
		{
			&nas_syslog("shareman", "naslog_warning", 31012, &text('sharename_already_tab', $in{'sharename'}));
			&error(&text('sharename_already_tab', $in{'sharename'}));
		}
		if (!(-e $in{'sharepath'}))
		{
			&nas_syslog("shareman", "naslog_warning", 31013, &text('sharepath_notexist', $in{'sharepath'}));
			&error(&text('sharepath_notexist', $in{'sharepath'}));
		}
	}
	if ($in{'sharetype'} =~ /W/)
	{
		if (($in{'sharename'} eq "global") || ($in{'sharename'} eq "homes"))
		{
			&nas_syslog("shareman", "naslog_warning", 31014, &text('sharename_illegal_cifs', $in{'sharename'}));
			&error(&text('sharename_illegal_cifs', $in{'sharename'}));
		}
		$findoldcifs = 0;
		if ($emode =~ /create/)
		{
			my $found = &check_cifs_config($in{'sharename'});
			if ($found)
			{
				&nas_syslog("shareman", "naslog_warning", 31015, &text('sharename_already_cifs', $in{'sharename'}));
				&error(&text('sharename_already_cifs', $in{'sharename'}));
			}
		}
		else
		{
			$findoldcifs = &check_cifs_config($in{'oldsharename'});
			if ($findoldcifs)
			{
				if ($in{'sharename'} ne $in{'oldsharename'})
				{
					if (&check_cifs_config($in{'sharename'}))
					{
						&nas_syslog("shareman", "naslog_warning", 31016, &text('sharename_already_cifs', $in{'sharename'}));
						&error(&text('sharename_already_cifs', $in{'sharename'}));
					}
				}
			}
			else
			{
				if (&check_cifs_config($in{'sharename'}))
				{
					&nas_syslog("shareman", "naslog_warning", 31017, &text('sharename_already_cifs', $in{'sharename'}));
					&error(&text('sharename_already_cifs', $in{'sharename'}));
				}
			}
		}
	}
	else
	{
		if ($emode =~ /property/)
		{
			if (&check_cifs_config($in{'oldsharename'}))
			{
				&foreign_require("samba", "samba-lib.pl");
				%econfig = &foreign_config("samba");
				&lock_file($econfig{'smb_conf'});
				@exps = &foreign_call("samba", "list_shares");
				foreach $s (@exps) {
					&foreign_call("samba", "get_share", $s);
					if (&foreign_call("samba", "getval", "copy") eq $in{'oldsharename'})
					{
						&foreign_call("samba", "delete_share", $s);
					}
				}
				&foreign_call("samba", "delete_share", $in{'oldsharename'});

				&unlock_file($econfig{'smb_conf'});
			}
		}
	}

	if ($in{'sharetype'} =~ /U/)
	{
		@checknfs = &check_nfs_config($in{'sharepath'});
		if ($emode =~ /create/)
		{
			if ($checknfs[0])
			{
				&nas_syslog("shareman", "naslog_warning", 31018, &text('sharepath_already_nfs', $in{'sharepath'}));
				&error(&text('sharepath_already_nfs', $in{'sharepath'}));
			}
		}
	}
	else
	{
		my @chknfs = &check_nfs_config($in{'sharepath'});

		if ($chknfs[0])
		{
			splice(@chknfs, 0, 1);
			&foreign_require("exports", "exports-lib.pl");
			%econfig = &foreign_config("exports");
			&lock_file($econfig{'exports_file'});
			&delete_export_by_indexes(\@chknfs);
			&unlock_file($econfig{'exports_file'});
		}
	}

	if ($in{'sharetype'} =~ /F/)
	{	
		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd", "ftp_ch_trustuser", $in{'sharepath'}, $in{'trustftpusercache'}, \@ulist);		
		$err_msg =  &foreign_call("pureftpd", "check_uglist", \@ulist);

		if($err_msg)
		{
			$err_msg =~ s/\\n/<p>/;
			$err_msg = "<p>" . $err_msg;
			&nas_syslog("shareman", "naslog_warning", $text{'log_ev_saveftp_checkuglist'}, &text('log_msg_saveftp_checkuglist', $in{'sharename'}));
			&error($err_msg)
		}
	}
	else
	{
		&foreign_require("pureftpd", "pureftpd-lib.pl");
		&foreign_call("pureftpd","get_pw_hash", \%pw);
		&foreign_call("pureftpd","get_sd_hash", \%sd);
		&foreign_call("pureftpd","get_gp_hash", \%gp);

		&foreign_call("pureftpd", "fetch_ftp_dir", $in{'sharepath'});

		&foreign_call("pureftpd", "share_ftp_reload", \%sd, \%pw, \%gp);
		&foreign_call("pureftpd", "sync_ftppasswd_with_getent", \%sd, \%pw);
		&foreign_call("pureftpd", "sync_ftpgroup_with_getent", \%gp);
	}

	if ($in{'sharetype'} =~ /H/)
	{
		#my @hfound = &check_http_config($in{'sharename'}, $in{'sharepath'});
		my $droot;
		@findoldhttp = (0, 0);
		if ($emode =~ /create/)
		{
			@findoldhttp = &check_http_config($in{'sharename'}, $in{'sharepath'});
			if ($findoldhttp[0])
			{
				&nas_syslog("shareman", "naslog_warning", 31020, &text('sharename_already_http', $in{'sharename'}));
				&error(&text('sharename_already_http', $in{'sharename'}));
			}
			$droot = $findoldhttp[2];
			if(-e $droot.'/'.$in{'sharename'})
			{
				&nas_syslog("shareman", "naslog_warning", $text{'log_ev_savehttp_shname'}, &text('log_msg_savehttp_shname', $in{'sharename'}));
				&error(&text('error_msg_savehttp_shname', $in{'sharename'}));
			}
		}
		else
		{
			@findoldhttp = &check_http_config($in{'oldsharename'}, $in{'sharepath'});
			if ($findoldhttp[0])
			{
				if ($in{'sharename'} ne $in{'oldsharename'})
				{
					my @checkhttp1 = &check_http_config($in{'sharename'}, $in{'sharepath'});
					if ($checkhttp1[0])
					{
						&nas_syslog("shareman", "naslog_warning", 31021, &text('sharename_already_http', $in{'sharename'}));
						&error(&text('sharename_already_http', $in{'sharename'}));
					}
					$droot = $findoldhttp[2];
					if(-e $droot.'/'.$in{'sharename'})
					{
						&nas_syslog("shareman", "naslog_warning", $text{'log_ev_savehttp_shname'}, &text('log_msg_savehttp_shname', $in{'sharename'}));
						&error(&text('error_msg_savehttp_shname', $in{'sharename'}));
					}
				}
			}
			else
			{
				my @checkhttp2 = &check_http_config($in{'sharename'}, $in{'sharepath'});
				if ($checkhttp2[0])
				{
					&nas_syslog("shareman", "naslog_warning", 31022, &text('sharename_already_http', $in{'sharename'}));
					&error(&text('sharename_already_http', $in{'sharename'}));
				}

				$droot = $findoldhttp[2];
				if(-e $droot.'/'.$in{'sharename'})
				{
					&nas_syslog("shareman", "naslog_warning", $text{'log_ev_savehttp_shname'}, &text('log_msg_savehttp_shname', $in{'sharename'}));
					&error(&text('error_msg_savehttp_shname', $in{'sharename'}));
				}
			}
		}
	}
	else
	{
		if ($emode =~ /property/)
		{
			my @chkhttp = &check_http_config($in{'oldsharename'}, $in{'sharepath'});
			if ($chkhttp[0])
			{
				&foreign_require("apache", "apache-lib.pl");
				my $d = $chkhttp[3];
				$f = $d->{'file'};
				&lock_file($f);
				my $lref = &read_file_lines($f);
				splice(@$lref, $d->{'line'}, $d->{'eline'}-$d->{'line'}+1);
				&flush_file_lines();
				&unlock_file($f);
			}
			my $droot = $chkhttp[2];
#			`rm -f $droot\/$in{'oldsharename'}`;
			ez_system("rm", "-f", $droot."\/".$in{'oldsharename'});
		}
	}

	if ($in{'sharetype'} =~ /A/)
	{	
		if(length($in{'afp_passwd'}) > 8)
		{
			&nas_syslog("shareman", "naslog_warning", $text{'log_ev_afppasswd_leng'}, &text('log_msg_afppasswd_leng', $in{'sharename'}));
			&error($text{'error_msg_afppasswd_leng'});
		}
	}
	else
	{
		&afp_del($in{'sharepath'});
	}

	if ($in{'sharetype'} =~ /W/)
	{
		&foreign_require("samba", "samba-lib.pl");
		%econfig = &foreign_config("samba");
		&lock_file($econfig{'smb_conf'});
		if ($findoldcifs)
		{
			&foreign_call("samba", "get_share", $in{'oldsharename'});
		}
		@trust_users = split(":", $in{'trustusercache'});
		&foreign_call("samba", "setval", "path", $in{'sharepath'});
		
		&foreign_call("samba", "setval", "browseable", "yes");
		&foreign_call("samba", "setval", "comment", $in{'comment'});
		&foreign_call("samba", "delval", "read only");
		if ($in{'othersel'} eq "1")
		{
			&foreign_call("samba", "setval", "writable", "yes");
		}
		else
		{
			&foreign_call("samba", "setval", "writable", "no");
		}
		#if ($in{'guestrdn'} eq "0")
		#{
		&foreign_call("samba", "delval", "public");
		&foreign_call("samba", "delval", "guest only");
		#}
		#elsif ($in{'guestrdn'} eq "1")
		#{
		#	&foreign_call("samba", "setval", "public", "yes");
		#	&foreign_call("samba", "delval", "guest only");
		#}
		#else
		#{
		#	&foreign_call("samba", "setval", "public", "yes");
		#	&foreign_call("samba", "setval", "guest only", "yes");
		#}
		&foreign_call("samba", "delval", "guest account");
		&foreign_call("samba", "delval", "only user");
		#if ($in{'hostallowrdn'}&& $in{'onlyallow'} =~ /\S/)
		#{
		#	&foreign_call("samba", "setval", "allow hosts", $in{'onlyallow'});
		#}
		#else
		#{
		&foreign_call("samba", "delval", "allow hosts");
		#}
		#if ($in{'hostdenyrdn'}&& $in{'onlydeny'} =~ /\S/)
		#{
		#	&foreign_call("samba", "setval", "deny hosts", $in{'onlydeny'});
		#}
		#else
		#{
		&foreign_call("samba", "delval", "deny hosts");
		#}
		#&foreign_call("samba", "setval", "revalidate", $in{'revalidrdn'});

		my @read_list, @write_list, @valid_list, @invalid_list;
		foreach $line (@trust_users) {
			@params = split(",", $line);
			$d = $params[1];
			if (($params[0] eq "1")||($params[0] eq "3"))
			{
				$d ="@".$d;
			}
			if ($d =~ /\s/)
			{
				if ($d =~ /(.*)(\\)(.*)/)
				{
					$d = $1.$2."\"".$3."\"";	
				}	
			}	
			push(@read_list, $d) if ($params[2] eq "0");
			push(@write_list, $d) if ($params[2] eq "1");
			push(@invalid_list, $d) if ($params[2] eq "2");
			if (($in{'othersel'} eq "2")&&($params[2] ne "2"))
			{
				push(@valid_list, $d);
			}

		}
		&foreign_call("samba", "setval", "valid users",
			join(',', @valid_list));
		&foreign_call("samba", "setval", "invalid users",
			join(',', @invalid_list));
		&foreign_call("samba", "setval", "user","");
		&foreign_call("samba", "setval", "read list",
			join(',', @read_list));
		&foreign_call("samba", "setval", "write list",
			join(',', @write_list));
			
		if ((@valid_list == 0)&&($in{'othersel'} eq "2"))
		{
			&foreign_call("samba", "setval", "available", "no");
		}
		else
		{
			&foreign_call("samba", "setval", "available", "yes");
		}
		if(!$in{'maxconnect'})
		{
			&foreign_call("samba", "setval", "max connections", 0);
		}
		else
		{
			($in{'maxconnectionin'} =~ /^\d+$/ && $in{'maxconnectionin'} > 0) ||(&nas_syslog("shareman", "naslog_warning", 31023, &text('invalid_number', $in{'maxconnectionin'})),
				 &error(&text('invalid_number', $in{'maxconnectionin'})));
			&foreign_call("samba", "setval", "max connections", $in{'maxconnectionin'});
		}

		&foreign_call("samba", "setval", "directory mode", "0777");
                &foreign_call("samba", "setval", "create mode", "0775");

		if ($findoldcifs)
		{
			&foreign_call("samba", "modify_share", $in{'oldsharename'}, $in{'sharename'});
		}
		else
		{
			&foreign_call("samba", "create_share", $in{'sharename'});
		}
		&unlock_file($econfig{'smb_conf'});
	}
	if ($in{'sharetype'} =~ /U/)
	{
		&foreign_require("exports", "exports-lib.pl");
		%econfig = &foreign_config("exports");
		&lock_file($econfig{'exports_file'});
		if ($checknfs[0])
		{
			splice(@checknfs, 0, 1);
			&delete_export_by_indexes(\@checknfs);
		}

		my @hostarray = split(':', $in{'trusthostcache'});
		my $h;
		for (my $i = 0 ; $i <  @hostarray; $i ++)
		{
			@hostentry = split(',',$hostarray[$i]);
#			print @hostentry;
			if ($hostentry[0] eq '0')
			{
				$h = "";
			}
			elsif ($hostentry[0] eq '1')
			{
				$h = $hostentry[1];
			}
			elsif ($hostentry[0] eq '2')
			{
				$h = "=public";
			}
			elsif ($hostentry[0] eq '3')
			{
				$h = "@" .$hostentry[1];
			}
			else
			{
				$h = $hostentry[1];
			}
			#print $h;
			$entry = { 'active' => 1,
				   'host' => $h,
				   'dir' => $in{'sharepath'}
				 };
			#$entry->{'options'}->{'link_relative'} = '' if ($hostentry[2] eq 'y');

			if ($hostentry[2] eq '0')
			{
				$entry->{'options'}->{'ro'} = '';
			}
			elsif ($hostentry[2] eq '1')
			{
				$entry->{'options'}->{'rw'} = '';
			}
			else
			{
				$entry->{'options'}->{'noaccess'} = '';
			}
			if ($hostentry[3] eq '0' )
			{
				$entry->{'options'}->{'no_root_squash'} = '';
			}
			elsif ($hostentry[3] eq '2')
			{
				$entry->{'options'}->{'all_squash'} = '';
			}
			&foreign_call("exports", "create_export", $entry);
		}
		&unlock_file($econfig{'exports_file'});

	}
	if ($in{'sharetype'} =~ /F/)
	{
		&foreign_require("pureftpd", "pureftpd-lib.pl");

		#&get_pw_hash(\%pw);
		#&get_sd_hash(\%sd);
		#&get_gp_hash(\%gp);

		&foreign_call("pureftpd","get_pw_hash", \%pw);
		&foreign_call("pureftpd","get_sd_hash", \%sd);
		&foreign_call("pureftpd","get_gp_hash", \%gp);

		&foreign_call("pureftpd", "fetch_ftp_dir", $in{'sharepath'});

		&foreign_call("pureftpd", "set_cfg_for_dir", \@ulist, $in{'sharepath'}, \%sd, \%pw);
		
		&foreign_call("pureftpd", "share_ftp_reload", \%sd, \%pw, \%gp);

		&foreign_call("pureftpd", "sync_ftppasswd_with_getent", \%sd, \%pw);

		&foreign_call("pureftpd", "sync_ftpgroup_with_getent", \%gp);
		&nas_syslog("shareman", "naslog_info", $text{'log_ev_saveftp_succ'}, &text('log_msg_saveftp_succ', $in{'sharename'}));
	}
	if ($in{'sharetype'} =~ /H/)
	{
		&foreign_require("apache", "apache-lib.pl");
		%econfig = &foreign_config("apache");
		#&foreign_call("apache", "lock_apache_files");
		my @values;
		push(@values, "Options FollowSymLinks Indexes");
		#if ($in{'checkorderrdn'} =~ /0/)
		#{
		#	push(@values, "order deny,allow");
		#}
		#elsif ($in{'checkorderrdn'} =~ /1/)
		#{
			push(@values, "order allow,deny");
		#}
		#elsif ($in{'checkorderrdn'} =~ /2/)
		#{
		#	push(@values, "order mutual-failure");
		#}
		#my @adlist = split(':', $in{'restrictlistcache'});
		#for (my $i = 0; $i < @adlist; $i++)
		#{
		#	@accesslist = split(',', $adlist[$i]);
		#	$temptext = "allow from " if ($accesslist[0] =~ /0/);
		#	$temptext = "deny from " if ($accesslist[0] =~ /1/);
		#	if ($accesslist[1] =~ /0/)
		#	{
		#		$temptext.="all";
		#	}
		#	elsif ($accesslist[1] =~ /6/)
		#	{
		#		$temptext.="env=$accesslist[2]";
		#	}
		#	else
		#	{
		#		$temptext.="$accesslist[2]";
		#	}
		#	push(@values, $temptext);
		#}
		push(@values, "allow from all");
		if ($in{'needauthrdn'} =~ /y/)
		{
			push(@values, "AllowOverride AuthConfig");
			#if ($in{'realmrdn'} =~ /1/)
			#{
				push(@values, "AuthName \"$gconfig{'mainbrand'}\"");
			#}
			push(@values, "AuthType Basic");
			#if ($in{'satisfyrdn'} =~ /1/)
			#{
			#	push(@values, "Satisfy all");
			#}
			#elsif ($in{'satisfyrdn'} =~ /2/)
			#{
				push(@values, "Satisfy all");
			#}
			if ($in{'accessloginrdn'} =~ /1/)
			{
				push(@values, "require user $in{'onlyusersin'}");
			}
			elsif ($in{'accessloginrdn'} =~ /2/)
			{
				push(@values, "require group $in{'onlygroupsin'}");
			}
			elsif ($in{'accessloginrdn'} =~ /0/)
			{
				push(@values, "require valid-user");
			}
			push(@values, "AuthUserFile $config{'authuser_file'}");
			push(@values, "AuthGroupFile $config{'authgroup_file'}");

		}
		my $droot = $findoldhttp[2];
		if ($findoldhttp[0])
		{
			my $d = $findoldhttp[3];
			$f = $d->{'file'};
			&lock_file($f);
			my $lref = &read_file_lines($f);
			my $newdir = "<Directory \"$droot\/$in{'sharename'}\">";
			my $enddir = "</Directory>";
#			$lref->[$d->{'line'}] = $newdir;
#			$lref->[$d->{'eline'}] = $enddir;
			unshift(@values, $newdir);
			push(@values, $enddir);
			splice(@$lref, $d->{'line'}, $d->{'eline'}-$d->{'line'}+1, @values);
			&flush_file_lines();
			&unlock_file($f);
		}
		else
		{
			my $vconf = &foreign_call("apache", "get_virtual_config", undef);
			$f = $vconf->[0]->{'file'};
			for ($j = 0; $vconf->[$j]->{'file'} eq $f; $j++) {}
			my $l = $vconf->[$j-1]->{'eline'} + 1;
			&lock_file($f);
			my $lref = &read_file_lines($f);
			my $newdir = "<Directory \"$droot\/$in{'sharename'}\">";
			my $enddir = "</Directory>";
			unshift(@values, $newdir);
			push(@values, $enddir);
			splice(@$lref, $l, 0, @values);

			&flush_file_lines();
			&unlock_file($f);
		}

#		`rm -f $droot\/$in{'oldsharename'}`;
#		`ln -s $in{'sharepath'} $droot\/$in{'sharename'}`;
		ez_system("rm", "-f", $droot."\/".$in{'oldsharename'});
		ez_system("ln", "-s", $in{'sharepath'}, $droot."\/".$in{'sharename'});

	}
	if ($in{'sharetype'} =~ /A/)
	{
		my %afp_volcfg;
		$afp_volcfg{'ro'} = 'yes' if ($in{'afp_mountRO'});
		$afp_volcfg{'password'} =  $in{'afp_passwd'} if ($in{'afp_passwd'});
		$afp_volcfg{'allowguest'} = 'yes' if ($in{'afp_allowguest'});
		
		&afp_save($in{'afptrustusercache'}, $in{'sharename'}, $in{'sharepath'}, \%afp_volcfg);
	}

	if ($anonypath)
	{
		@anoninfo = &search_shareinfo($anonypath);
		if ($anoninfo[1] eq "F")
		{
			&save_sharetab(2, $anonypath);
		}
		else
		{
			$anoninfo[1] =~ s/F//;
			&save_sharetab(3, $anonypath, $anoninfo[0], $anoninfo[1], $anoninfo[2]);
		}
	}
	if ($emode =~ /create/)
	{
		&save_sharetab(1, $in{'sharepath'}, $in{'sharename'}, $in{'sharetype'}, $in{'comment'});
	}
	else
	{
		if ($a)
		{
			&save_sharetab(3, $in{'sharepath'}, $in{'sharename'}, $in{'sharetype'}, $in{'comment'});
		}
		else
		{
			&save_sharetab(1, $in{'sharepath'}, $in{'sharename'}, $in{'sharetype'}, $in{'comment'});
		}
	}
#-----------chenmo: rewrite all user smb conf---------------#
        @localusers = get_localuser();
        foreach $luu (@localusers){
           &write_user_conf($luu);
        }
#-----------------------------------------------------------#
	`$config{'cifs_reload'}`;
	`$config{'nfs_reload'}`;
	`$config{'ftp_reload'}`;
	`$config{'http_reload'}`;
	&header($text{'shares_title'}, "", undef, 0, 1);
#	print "<hr>\n";
	print $text{'save_share_success'};
	&nas_syslog("shareman", "naslog_info", 31024, "Save share success!"); 
#	print "<hr>\n";
	$in{'sharepath'} =~ /(.*\/)[^\/]+$/;
	&footer("index.cgi",  $text{'list_shares_return'}, "../folderman/index.cgi?dir=$1",  $text{'list_folders_return'}, "../protocolman/index.cgi", $text{'list_protocols_return'});

	#&redirect("index.cgi");
}
